const express = require('express');
const router = express.Router();
const db = require('../db/index')
// const manageDb = require('../db/manage')
const fs = require('fs');
const path = require('path')
const bcrypt = require('bcryptjs');
const upload = require('../untils/upload.config')
const { verify, jwt, SECRET_KEY } = require('../untils/tokenVerify')
let Ffmpeg = require("fluent-ffmpeg");
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
Ffmpeg.setFfmpegPath(ffmpegPath);


router.post('/login', (req, res) => {
    const { name, password } = req.body;
    if (!name || !password) return res.send({ code: 200, msg: '登录失败！' });
    const sql = `select * from user where name=?`
    db.query(sql, name, (err, result) => {
        if (err) {
            return res.send({ code: 500, msg: err })
        }
        const ps = `${password}`.trim();
        // const isMatch = bcrypt.compareSync(ps, result[0].password);
        const isMatch = result[0].password === password;

        if (isMatch) {
            const { avatar } = result[0]
            const token = jwt.sign(
                { user: { name, password } },
                SECRET_KEY,
                { expiresIn: '720h' }
            )
            return res.send({
                code: 200,
                data: {
                    token,
                    name,
                    avatar,
                }, msg: '登陆成功！'
            })
        } else {
            return res.send({ code: 200, msg: '账号或密码错误！' })
        }

    })

});

// 注册
router.post('/logon', (req, res) => {
    const { name, password, confirm } = req.body;
    if (!name || !password || !confirm) return res.send({ code: 200, msg: '注册失败！' });
    if (confirm !== password) return res.send({ code: 500, msg: '密码不一致 !' })
    const ps = `${password}`.trim();
    const salt = bcrypt.genSaltSync(10);
    const hash = bcrypt.hashSync(ps, salt);
    const sql = `select * from user where name=?`
    db.query(sql, name, (err, result) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else if (result && result.length) {
            res.send({ code: 500, msg: '用户已存在！' })
        } else {
            const sqlAdd = `insert into user (name,password) values (?,?)`
            db.query(sqlAdd, [name, hash], (err, result) => {
                if (err) {
                    res.send({ code: 500, msg: err })
                } else {
                    res.send({ code: 200, msg: '注册成功！' })
                }
            })
        }
    })
});

router.get('/menu', (req, res) => {
    const sql = `select * from menu`
    db.query(sql, (err, result) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({
                code: 200,
                data: result,
                msg: '查询成功！'
            })
        }
    })
});

// 获取文章列表
router.post('/article/list', (req, res) => {
    const { pageNum = 1, pageSize = 10, menuId } = req.body;
    const params = [(parseInt(pageNum) - 1) * parseInt(pageSize), parseInt(pageSize)]
    const sql = `select title,id from article where menuId=? limit ?,?`
    db.query(sql, [menuId, ...params], (err, result) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({
                code: 200,
                data: result,
                msg: '查询成功！'
            })
        }
    })
});

// 获取文章详情
router.get('/article/detail', (req, res) => {
    const { id } = req.query
    const sql = `select title,content from article where id=?`
    db.query(sql, id, (err, result) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({
                code: 200,
                data: result[0],
                msg: '查询成功！'
            })
        }
    })
});


// 发布文章
router.post('/article/post', (req, res) => {
    const { title, content, menuId } = req.body
    if (!title || !content || !menuId) return res.send({ code: 500, msg: '请填写完整后提交！' })
    const sql = `insert into  article (title,content,menuId) values (?,?,?)`
    db.query(sql, [title, content, menuId], (err, result) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({
                code: 200,
                msg: '发布成功！'
            })
        }
    })
});

module.exports = router
